package com.anycl.design.creation.prototype;

import org.junit.Test;

import java.util.Date;

public class Main {

    @Test
    public void test() {
        Date date = new Date();
        Person p1 = new Person(23, date);
        Person p2 = p1;
        System.out.println(p1);
        System.out.println(p2);

    }

    @Test
    public void testClone() throws CloneNotSupportedException {
        Date date = new Date();
        Person p1 = new Person(23, date);
        Person p2 = (Person) p1.clone();
        System.out.println(p1);
        System.out.println(p2);

        System.out.println(p1.getAge() == p2.getAge());
        date.setTime(234234234L);
        System.out.println(p1.getBirth());
        System.out.println(p2.getBirth());
        // return super.clone();
        // birth引用类型，浅拷贝，两个birth指向同一个Date对象
        System.out.println(p1.getBirth() == p2.getBirth());

    }


    @Test
    public void testClone2() throws CloneNotSupportedException {
        Date date = new Date();
        Address address = new Address(new Code());
        Person p1 = new Person(23, date, address);
        Person p2 = (Person) p1.clone();
        System.out.println(p1 == p2);
        System.out.println(p1.getAddress() == p2.getAddress());
        System.out.println(p1.getAddress().getCode() == p2.getAddress().getCode());

    }
}
